package com.astrofizzbizz.whatchadoing;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;

import com.astrofizzbizz.emailtextfile.EmailTextFile;
import com.astrofizzbizz.emailtextfile.EmailTextFile.EmailTextFileException;
import com.astrofizzbizz.utilities.DateUtilties;
import com.astrofizzbizz.utilities.DpmSwingUtilities;

public class WhatchaDoing implements Runnable
{
	private Date	startDate;
	private Date	stopDate;
	private int		numNotifyPerDay = 5;
	private String	emailName = null;
	private	String	emailAddress = null;
	private	String	emailPassword = null;
	private	String	emailSubject = "What are you doing?";
	private String	emailText = "What are you doing? \n\nWhere are you?\n\nWho are you with?\n\nHow do you feel\n\n";
	private boolean	stopPestering = true;
	private int		idate = 0;
	private WhatchaDoingException error = null;
	private WhatchaDoingGui whatchaDoingGui = null;

	public Exception getError() {return error;}
	public Date		getStartDate()  {return(startDate);}
	public Date		getStopDate() {return(stopDate);}
	public int 		getNumNotifyPerDay() {return numNotifyPerDay;}
	public String 	getEmailName() {return emailName;}
	public String 	getEmailAddress() {return emailAddress;}
	public String 	getEmailPassword() {return emailPassword;}
	public String 	getEmailSubject() {return emailSubject;}
	public String 	getEmailText() {return emailText;}
	public boolean 	isStopPestering() {return stopPestering;}

	public void setStartDate(Date startDate) {this.startDate = startDate;}
	public void setStopDate(Date stopDate) {this.stopDate = stopDate;}
	public void setNumNotifyPerDay(int numNotifyPerDay) {this.numNotifyPerDay = numNotifyPerDay;}
	public void setEmailName(String emailName) {this.emailName = emailName;}
	public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;}
	public void setEmailPassword(String emailPassword) {this.emailPassword = emailPassword;}
	public void setEmailSubject(String emailSubject) {this.emailSubject = emailSubject;}
	public void setEmailText(String emailText) {this.emailText = emailText;}
	public void setStopPestering(boolean stopPestering) {this.stopPestering = stopPestering;}
	public void setWhatchaDoingGui(WhatchaDoingGui whatchaDoingGui) {this.whatchaDoingGui = whatchaDoingGui;}
	
	public WhatchaDoing(Date startDate) throws WhatchaDoingException
	{
		int[] dayMonthYearHourMinuteSecond = DateUtilties.dayMonthYearHourMinuteSecond(startDate);
		dayMonthYearHourMinuteSecond[3] = 7;
		dayMonthYearHourMinuteSecond[4] = 30;
		dayMonthYearHourMinuteSecond[5] = 0;
		try {this.startDate = DateUtilties.makeDate(dayMonthYearHourMinuteSecond);} 
		catch (ParseException e) {throw new WhatchaDoingException(e);}
		dayMonthYearHourMinuteSecond[0] = dayMonthYearHourMinuteSecond[0] + 6;
		dayMonthYearHourMinuteSecond[3] = 22;
		dayMonthYearHourMinuteSecond[4] = 0;
		dayMonthYearHourMinuteSecond[5] = 0;
		try {this.stopDate = DateUtilties.makeDate(dayMonthYearHourMinuteSecond);} 
		catch (ParseException e) {throw new WhatchaDoingException(e);}
	}
	public WhatchaDoing() throws WhatchaDoingException
	{
		this(new Date());
	}
	int  getNoOfDays()
	{
		long ldeltaTime = (stopDate.getTime() - startDate.getTime()) / 1000;
		double deltaDays = (double) ldeltaTime;
		deltaDays = deltaDays / (3600.0 * 24.0);
		return ((int) deltaDays) + 1;
	}
	Date generateRandomDate() throws WhatchaDoingException 
	{
		double randy = Math.random() * ((double) getNoOfDays() * 0.99999999);
		int numMinPerDay = (DateUtilties.getHours(stopDate) * 60 + DateUtilties.getMins(stopDate))  
				- (DateUtilties.getHours(startDate) * 60 + DateUtilties.getMins(startDate));
		if (numMinPerDay <= 0 ) throw new WhatchaDoingException("End time before Start time!");
		int day = (int) randy;
		double dminFromStart = (randy - ((double) day)) * ((double) numMinPerDay);
		int iminFromStart = (int) dminFromStart;
		try {
			return DateUtilties.makeDate(
					day + DateUtilties.getDay(startDate), DateUtilties.getMonth(startDate), 
					DateUtilties.getYear(startDate), 
					DateUtilties.getHours(startDate), DateUtilties.getMins(startDate) + iminFromStart, 0);
		} catch (ParseException e) {
			throw new WhatchaDoingException(e);
		}
	}
	Date[] generateDateList() throws WhatchaDoingException 
	{
		int numDates = getNoOfDays() * getNumNotifyPerDay();
		Date[] dateList = new Date[numDates];
		for (int ii = 0; ii < numDates; ++ii)
		{
			dateList[ii]  = generateRandomDate();
		}
		Arrays.sort(dateList);
		for (int ii = 0; ii < numDates; ++ii)
		{
//			System.out.println("Date "+ ii + " = " + dateList[ii].toString());
		}

		return dateList;
	}
	void sendEmail() throws WhatchaDoingException 
	{
		EmailTextFile email = new EmailTextFile();
		emailSubject = "What are you doing on " + new Date().toString() + " ?";
		emailText = "Hej " + emailName + ",\n\n";
		emailText = emailText 
				+ emailSubject + "\n\nWhere are you?\n\nWho are you with?\n\nHow do you feel?\n\nTack!\n";

		email.setSubject(emailSubject);
		email.setFromAddress(emailName, emailAddress);
		email.addRecipient(emailName, emailAddress);
		email.setText(emailText);
		try {
			email.sendGmail(emailAddress, emailPassword);
		} catch (EmailTextFileException e) {
			throw new WhatchaDoingException(e);
		}

	}
	void sendPesterListToEmail(Date[] dateList, int istart) throws WhatchaDoingException 
	{
		EmailTextFile email = new EmailTextFile();
		emailSubject = "Whatcha Doing Pester List";
		emailText = "";
		for (int ii = istart; ii < dateList.length; ++ii)
		{
			emailText = emailText + "Date "+ ii + "\t" + dateList[ii].toString() + "\n";
		}

		email.setSubject(emailSubject);
		email.setFromAddress(emailName, emailAddress);
		email.addRecipient(emailName, emailAddress);
		email.setText(emailText);
		try {
			email.sendGmail(emailAddress, emailPassword);
		} catch (EmailTextFileException e) {
			throw new WhatchaDoingException(e);
		}

	}

	@Override
	public void run() 
	{
		try 
		{ 
			boolean printNotify = true;
			error = null;
			stopPestering = true;
			Date[] dateList = null;
			dateList = generateDateList();
			idate = 0;
			while (stopPestering)
			{
				if (new Date().getTime() > dateList[idate].getTime())
				{
					idate = idate + 1;
					if (idate >= dateList.length) throw new WhatchaDoingException("Date List is old");
				}
				else
				{
					stopPestering = false;
				}
			}
			sendPesterListToEmail(dateList, idate);
			while (!stopPestering)
			{
				if (new Date().getTime() > dateList[idate].getTime())
				{
					if (whatchaDoingGui != null ) 
					{
						whatchaDoingGui.statusBar.setText("Sending out pester message.");
					}
					sendEmail();
					idate = idate + 1;
					printNotify = true;
					if (idate >= dateList.length) stopPestering = true;
				}
				else
				{
					if (printNotify)
					{
						if (whatchaDoingGui != null )
							whatchaDoingGui.statusBar.setText("Next Pester at " + dateList[idate].toString());
						printNotify = false;
					}
					try {Thread.sleep(2000);} catch (InterruptedException e) {}
				}
			}
			if (whatchaDoingGui != null ) 
			{
				whatchaDoingGui.statusBar.setText("Finished.");
				whatchaDoingGui.stopPestering();
			}
		}
		catch (WhatchaDoingException e) 
		{
			error = e;
			if (whatchaDoingGui != null ) 
			{
				whatchaDoingGui.stopPestering();
				DpmSwingUtilities.messageDialog(error.getMessage(), whatchaDoingGui);
			}
			return;
		}
	}
	public class WhatchaDoingException extends Exception
	{
		private static final long serialVersionUID = -8814322029222070205L;
		private WhatchaDoingException(String message)
		{
			super(message);
		}
		private WhatchaDoingException(Exception e)
		{
			super(e);
		}
	}
	public static void main(String[] args) throws WhatchaDoingException 
	{
		WhatchaDoing whatchaDoing = new WhatchaDoing();
		whatchaDoing.setNumNotifyPerDay(10);
		whatchaDoing.setEmailAddress("dmcginnis427@gmail.com");
		whatchaDoing.setEmailPassword("Zebo<>graf");
		
		System.out.println("Start Date = " + whatchaDoing.getStartDate().toString());
		System.out.println("Stop  Date = " + whatchaDoing.getStopDate().toString());
//		Thread thread = new Thread(whatchaDoing);
//		thread.start();
		whatchaDoing.generateDateList();
	}

}
